package com.vicene.gmail.order.interceptor;

import com.vicene.gmail.common.vo.WeiBoLoginResp;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    public static ThreadLocal<WeiBoLoginResp> loginUser = new ThreadLocal<WeiBoLoginResp>();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
       // System.out.println("333");

        String uri = request.getRequestURI();
        boolean match = new AntPathMatcher().match("/order/order/status/**" , uri);

        boolean matchPay = new AntPathMatcher().match("/payed/**" , uri);

        if(match || matchPay){
            return true;
        }

        WeiBoLoginResp attribute = (WeiBoLoginResp) request.getSession().getAttribute("loginUser");

        if(attribute == null){
            response.sendRedirect("http://auth.gmail.com/login.html");
            return false;
        }
        loginUser.set(attribute);
        return true;
    }
}
